<template >
  <div>
    <h1>微信网页授权</h1>
    <img v-if="code"
      style="width: 100px; height: 100px"
      :src="userInfo.headimgurl"
      alt=""
    />
    <p>{{ userInfo.nickname }}</p>
    
  </div>
</template>

<script>
import axios from 'axios'
const serveHost = 'http://www.bufantec.com'
// 优先测试 snsapi_userinfo 功能
const appid = `wxc48cd51ac86c9608`
const redirectUrl = `http://s28r45.natappfree.cc/wx`
const userInfoUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirectUrl}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
export default {
  data() {
    return {
      code: '',
      userInfo: ''
    }
  },
  created() {
    // 尝试从url获取code
    this.code = this.$route.query.code
    if (this.code) {
      console.log(this.code)
       var url = `${serveHost}/wx/user/getUserInfo?appid=${appid}&code=${this.code}`
                      // - 参数: appid
                      // - 参数: code
                  axios.get(url)
                      .then(res=>{
                          this.userInfo = res.data.data
                          console.log(res);
                      })
    } else {
      setTimeout(() => {
        window.location.href = userInfoUrl
      }, 2000)
    }
  },
  methods: {}
}
</script>

<style lang="scss" scoped>
</style>